package com.cqc.processmanagerdemo;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 进程管理
 */
public class MainActivity extends AppCompatActivity {


    @BindView(R.id.layout_loading)
    LinearLayout layoutLoading;
    @BindView(R.id.tv_process_count)
    TextView tvProcessCount;
    @BindView(R.id.tv_mem)
    TextView tvMem;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_no_all)
    TextView tvNoAll;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;

    private Context context = this;
    private List<ProcessInfo> userProList = new ArrayList<>();
    private List<ProcessInfo> sysProList = new ArrayList<>();
    private ProcessAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //准备假数据
        //        initData();

        tvProcessCount.setText("进程数量：" + ProcessUtil.getProcessCount(context));
        String avaibleMem = Formatter.formatFileSize(context, ProcessUtil.getAvailMem(context));
        String totalMem = Formatter.formatFileSize(context, ProcessUtil.getTotalMem(context));
        tvMem.setText("可用内存:" + avaibleMem + ",总共：" + totalMem);


        new AsyncTask<Void, Void, Void>() {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                layoutLoading.setVisibility(View.VISIBLE);
                layoutContent.setVisibility(View.GONE);
            }

            @Override
            protected Void doInBackground(Void... params) {
                List<ProcessInfo> processList = ProcessUtil.getProcessList(context);
                for (ProcessInfo info : processList) {
                    if (info.isSystem()) {
                        sysProList.add(info);
                    } else {
                        userProList.add(info);
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                adapter = new ProcessAdapter(context, userProList, sysProList);
                listView.setAdapter(adapter);
                initListener();

                layoutLoading.setVisibility(View.GONE);
                layoutContent.setVisibility(View.VISIBLE);
            }
        }.execute();

    }

    private void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProcessAdapter.ViewHolder holder = (ProcessAdapter.ViewHolder) view.getTag();

                ProcessInfo info = null;
                if (position > 0 && position <= userProList.size()) {
                    info = userProList.get(position - 1);
                } else if (position > (userProList.size() + 1)) {
                    info = sysProList.get(position - 2 - userProList.size());
                }
                info.setCheck(!info.isCheck());
                holder.cb.setChecked(info.isCheck());
            }
        });
    }


    public void click(View view) {
        switch (view.getId()) {
            case R.id.tv_clear:

                break;
            case R.id.tv_all:
                for (int i = 0; i < userProList.size(); i++) {
                    userProList.get(i).setCheck(true);
                }
                for (int i = 0; i < sysProList.size(); i++) {
                    sysProList.get(i).setCheck(true);
                }
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.tv_no_all:
                for (ProcessInfo info : sysProList) {
                    info.setCheck(!info.isCheck());
                }
                for (ProcessInfo info : userProList) {
                    info.setCheck(!info.isCheck());
                }
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.tv_set:

                break;
        }
    }

    /**
     * 假数据
     */
    private void initData() {
        for (int i = 0; i < 5; i++) {
            ProcessInfo info = new ProcessInfo();
            info.setPackageName("com.tencent.qq");
            info.setName("QQ" + i);
            info.setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
            info.setSystem(false);
            info.setMemorySize(1024 * 1024 * 10 + i * 100);
            info.setPid(100 + i);
            userProList.add(info);
        }
        for (int i = 0; i < 10; i++) {
            ProcessInfo info = new ProcessInfo();
            info.setPackageName("com.ali.taobao");
            info.setName("淘宝" + i);
            info.setIcon(getResources().getDrawable(R.mipmap.ic_launcher_round));
            info.setSystem(true);
            info.setMemorySize(1024 * 1024 * 10 + i * 100);
            info.setPid(200 + i);
            sysProList.add(info);
        }
    }
}
